package com.link.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.ServletActionContext;

import com.common.util.EncriptUtil;
import com.link.po.Account;
import com.others.util.JSONParameter;

/**
 * @author Morgan Liao
 * 
 */
@SuppressWarnings("serial")
public class AJAXAction extends BaseAction {
    
	public String execute() {
		return SUCCESS;
	}

    public String updateNumClick() throws IOException {
        
        String linkIdStr = getLinkId();
        
        int linkId = 0;
        try {
            linkId = Integer.parseInt(linkIdStr);
        } catch (NumberFormatException e) {
            linkId = 0;
        }
        
        if (linkId == 0 || linkId > 9999) {
            return NONE;
        }
        
        getLinkService().updateNumClick(linkId);
        
        List<JSONParameter> emptyList = new ArrayList<JSONParameter>();
        
        JSONArray jsonData = JSONArray.fromObject(emptyList);
        initAJAX(jsonData);

        return NONE;
    }
    
    
    public String changePassword() throws IOException {

        Account account = (Account) getSession().get(ACCOUNT_SESSION);
        if (account == null) {
            List<JSONParameter> errorMessage = new ArrayList<JSONParameter>();
            JSONParameter jsonParameter = new JSONParameter();
            jsonParameter.setKey("0");
            jsonParameter.setValue(getText(ERROR_NOT_LOGIN));
            errorMessage.add(jsonParameter);
            
            JSONArray jsonData = JSONArray.fromObject(errorMessage);
            
            initAJAX(jsonData);
            
            return NONE;
        }
        
        if (StringUtils.isBlank(getNewPassword())) {
            List<JSONParameter> errorMessage = new ArrayList<JSONParameter>();
            JSONParameter jsonParameter = new JSONParameter();
            jsonParameter.setKey("0");
            List<Object> args = new ArrayList<Object>();
            args.add("New Password");
            jsonParameter.setValue(getText(ERROR_NOT_LOGIN, args));
            errorMessage.add(jsonParameter);
            
            JSONArray jsonData = JSONArray.fromObject(errorMessage);
            
            initAJAX(jsonData);
            
            return NONE;
        }
        
        account.setPassword(EncriptUtil.sha256Encode(getNewPassword().trim()));

        getAuthenticationService().updateAccount(account);
        
        List<JSONParameter> emptyList = new ArrayList<JSONParameter>();
        
        JSONArray jsonData = JSONArray.fromObject(emptyList);
        initAJAX(jsonData);
        
        return NONE;
    }
    
    
    
    
    private void initAJAX (JSONArray jsonData) throws IOException {
        HttpServletResponse response = ServletActionContext.getResponse();
        response.setContentType("text/html;charset=UTF-8");
        response.setHeader("Cache-Control", "no-cache");
        PrintWriter out = response.getWriter();
        out.print(jsonData);
        out.close();
    }
    
    private String linkId;
    private String newPassword;

    public String getLinkId() {
        return linkId;
    }

    public void setLinkId(String linkId) {
        this.linkId = linkId;
    }

    public String getNewPassword() {
        return newPassword;
    }

    public void setNewPassword(String newPassword) {
        this.newPassword = newPassword;
    }


    
    
    
}